package com.boylegu.springboot_vue.controller;

import com.boylegu.springboot_vue.entities.Users;
import com.boylegu.springboot_vue.service.UserService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.PageRequest;

import com.boylegu.springboot_vue.controller.pagination.PaginationMultiTypeValuesHelper;
import com.boylegu.springboot_vue.controller.pagination.PaginationFormatting;

import java.util.*;


@RestController
@RequestMapping("/api/users")
public class UsersMainController {

    @Autowired
    private UserService userService;

    @Value(("${com.boylegu.paginatio.max-per-page}"))
    Integer maxPerPage;

//    @RequestMapping(value = "/sex", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
//    public ResponseEntity<?> getSexAll() {
//
//        /*
//         * @api {GET} /api/persons/sex Get all sexList
//         * @apiName GetAllSexList
//         * @apiGroup Info Manage
//         * @apiVersion 1.0.0
//         * @apiExample {httpie} Example usage:
//         *
//         *     http /api/persons/sex
//         *
//         * @apiSuccess {String} label
//         * @apiSuccess {String} value
//         */
//
//        ArrayList<Map<String, String>> results = new ArrayList<>();
//
//        for (Object value : personsRepository.findSex()) {
//
//            Map<String, String> sex = new HashMap<>();
//
//            sex.put("label", value.toString());
//
//            sex.put("value", value.toString());
//
//            results.add(sex);
//        }
//
//        ResponseEntity<ArrayList<Map<String, String>>> responseEntity = new ResponseEntity<>(results,
//                HttpStatus.OK);
//
//        return responseEntity;
//    }

    @RequestMapping(method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public Map<String, PaginationMultiTypeValuesHelper> getUsersAll(
            @RequestParam(value = "page", required = false) Integer pages
    ) {
        if (pages == null) {
            pages = 1;
        }
        Sort sort = new Sort(Direction.ASC, "id");

        Pageable pageable = new PageRequest(pages - 1, maxPerPage, sort);

        PaginationFormatting paginInstance = new PaginationFormatting();

        return paginInstance.filterQuery(pageable, PaginationFormatting.TYPE_USER);
    }

    @RequestMapping(value = "/detail/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Users> getDetail(@PathVariable String id) {

        Users user = new Users();
        if (id != null && !"new".equalsIgnoreCase(id)) {
            user = userService.findById(id);
        }

        return new ResponseEntity<>(user, HttpStatus.OK);
    }

    @RequestMapping(value = "/detail/{id}", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE)
    public Users update(@PathVariable String id, @RequestBody Users data) {

        Users user = userService.findById(id);

        // TODO put what fields can be edit here

        user.setStudent_id(data.getStudent_id());

        user.setTeacher_id(data.getTeacher_id());

        return userService.save(user);
    }

    @RequestMapping(value = "/create/{id}", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE)
    public Users create(@RequestBody Users data) {

        Users result = null;

        // TODO validate user data
        try {
            result = userService.save(data);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_VALUE)
    public HttpStatus delete(@PathVariable String id) {

        userService.deleteById(id);

        return HttpStatus.OK;
    }
}